package com.tklab.smartcity.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.tklab.smartcity.App;
import com.tklab.smartcity.R;
import com.tklab.smartcity.bean.ServiceBean;
import com.tklab.smartcity.util.RetrofitUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ServiceAdapter extends RecyclerView.Adapter<ServiceAdapter.ViewHolder>{

//    数据源
    private List<ServiceBean.RowsBean> list;

    public ServiceAdapter(List<ServiceBean.RowsBean> list){
        this.list=list;
    }
    @NotNull
    @Override
    public ServiceAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(App.context).inflate(R.layout.home_service_adpter_layout,
                null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull ViewHolder holder, int position) {
        if(position+1==list.size()){
            holder.txtServiceName.setText("更多服务");
            holder.imageView.setImageResource(R.drawable.ic_launcher_background);
        }else {
            ServiceBean.RowsBean r=list.get(position);
            holder.txtServiceName.setText(r.getServiceName());
            String url= RetrofitUtil.BASE_URL+r.getImgUrl();
            Glide.with(App.context).load(url).into(holder.imageView);
        }

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ImageView imageView;
        public TextView txtServiceName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imgService);
            txtServiceName=itemView.findViewById(R.id.txtServiceName);
        }
    }
}
